package jbox2dprocessing.compontent;

import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import pbox2d.PBox2D;
import processing.core.PApplet;
import processing.core.PConstants;

public class Box {
	
	public static PApplet parent;
	
	Body body;
	
	float w;
	float h;
	PBox2D box2d;
	
	public Box(float x, float y, PBox2D _box2d) {
		super();
		this.w = parent.random(4.0f, 16.0f);
		this.h = parent.random(4.0f, 16.0f);
		this.box2d = _box2d;
		makeBody(new Vec2(x, y), w, h);
	}
	
	public void display(){
		Vec2 pos = box2d.getScreenPos(body);
		float a = body.getAngle();
		
		parent.rectMode(PConstants.CENTER);
		parent.pushMatrix();
		parent.translate(pos.x, pos.y);
		parent.rotate(a);
		parent.fill(175);
		parent.rect(0, 0, w, h);
		parent.popMatrix();
	}
	
	public void makeBody(Vec2 center, float _w, float _h){
		PolygonDef sd = new PolygonDef();
		float box2dW = box2d.scaleScreenToWorld(_w/2);
		float box2dH = box2d.scaleScreenToWorld(_h/2);
		sd.setAsBox(box2dW, box2dH);
		
		sd.density = 1.0f;
		sd.friction = 0.3f;
		sd.restitution = 0.5f;
		
		BodyDef bd = new BodyDef();
		bd.position.set(box2d.screenToWorld(center));
		
		body = box2d.createBody(bd);
		body.createShape(sd);
		body.setMassFromShapes();
		
		body.setLinearVelocity(new Vec2(parent.random(-5,5), parent.random(2, 5)));
		body.setAngularVelocity(parent.random(-5, 5));
	}
}




















